<template>
    <div>
      <el-dialog
        title="销售填写"
        :visible.sync="dialogAddCartVisible"
        width="50%">

        <el-form :model="form" :rules="rules"  ref="form" label-width="100px" class="demo-ruleForm">

        <el-form-item
          label="购买数量"
          prop="num"
        >
          <el-input type="num" v-model.number="form.num" autocomplete="off"></el-input>
        </el-form-item>


          <el-form-item
            label="购买客户"
            prop="customerIds"
          >
            <el-select v-model="form.customerIds" multiple style="width: 100%" filterable placeholder="请选择">
              <el-option
                v-for="item in customerList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>


          <el-form-item>
            <el-button type="primary" @click="_addToCart('form')">添加</el-button>
            <el-button @click="dialogAddCartVisible = false">关闭</el-button>
          </el-form-item>

        </el-form>





      </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "GoodsProductAddCart",

      data(){
          return{
            dialogAddCartVisible:false,
            form:{
              num:'',
              productId:'',
              customerIds:'',
            },
            customerList:[],


            rules: {
              customerIds: [
                { required: true, message: '请选择购买客户', trigger: 'change' }
              ],
             num: [
               { required: true, message: '购买数量不能为空'},
               { type: 'number', message: '采购数量必须为数字值'}]
            }

          }
      },
      methods:{
        _open(id){
          this.form.num='';
          this.form.productId=id;
          this.form.customerIds='';
          this.dialogAddCartVisible=true;

          this._customerListInit();
        },


        _customerListInit(){
          this.$http.get('/t_customer/list_customer').then(res=>{
            this.customerList=res.data.data;
          }).catch(err=>{

          });
        },

        _addToCart(formName) {
          this.$refs[formName].validate((valid) => {
            if (valid) {

              this.$http.post("/t_sales/add_sales?productId="+this.form.productId+'&num='+this.form.num+'&customerIds='+this.form.customerIds).then(result=>{
                console.log("result:"+result)
                this.$message('添加成功');
                this.dialogAddCartVisible=false;

              }).catch(err=>{
                console.log(err);
              })


            } else {
              console.log('error submit!!');
              return false;
            }
          });
        },



      },
    }
</script>

<style scoped>

</style>
